﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class UserMng : RelationsLMP.Control.Form.MetroDemoForms
    {
        public UserMng()
        {
            InitializeComponent();
            this.Text = "人员管理";

            this.FormClosing += UserMng_FormClosing;

            InitControls();

            groupBoxX2.Visible = false;

            btnSearch_Click(null, null);
        }

        private void InitControls()
        {
            try
            {
                Static.Methods.CreateDGVColumns(ref dgvUserInfo, Model.SystemEnum.DGVType.UserInfo);
                dgvUserInfo.CellClick += DgvUserInfo_CellClick;

                InitComBoxRoleInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "初始化控件失败，错误信息：" + ex.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void DgvUserInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sender != null && e.RowIndex >= 0)
            {
                if (dgvUserInfo.Rows[e.RowIndex].Cells["Selected"].Value == null || !(bool)dgvUserInfo.Rows[e.RowIndex].Cells["Selected"].Value)
                    dgvUserInfo.Rows[e.RowIndex].Cells["Selected"].Value = true;
                else
                    dgvUserInfo.Rows[e.RowIndex].Cells["Selected"].Value = false;
            }
        }

        private void InitComBoxRoleInfo()
        {
            cmbRole.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbRole.DisplayMember = "Name";
            cmbRole.ValueMember = "RoleId";

            List<Model.DB.tb_RoleInfo> _List;
            _List = new DataAccess.tb_RoleInfo().GetRoleInfoList();
            _List.Insert(0, new Model.DB.tb_RoleInfo() { RoleId = -1, Name = "全部" });

            cmbRole.DataSource = _List;

            if (cmbRole.Items.Count > 0)
            {
                cmbRole.SelectedIndex = 0;
            }
        }

        private void UserMng_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (fingerPrintMng != null)
                fingerPrintMng.Disposed();
        }

        Relations.Control.UC.fingerPrintMng fingerPrintMng;

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> _LogisticList = GetLogisticList();
            List<Model.DB.tb_User> _List;
            try
            {
                _List = new DataAccess.tb_User().GetUserInfoList(_LogisticList);
                dgvUserInfo.DataSource = _List;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "查询用户信息失败，错误信息：" + ex.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> _IndexList = new List<string>();
            bool _Select;
            for (int i = 0; i < dgvUserInfo.Rows.Count; i++)
            {
                if (dgvUserInfo.Rows[i].Cells["Selected"].Value == null) continue;
                _Select = (bool)dgvUserInfo.Rows[i].Cells["Selected"].Value;
                if (_Select)
                    _IndexList.Add(dgvUserInfo.Rows[i].Cells["userId"].Value.ToString());
            }

            if (_IndexList.Count <= 0)
            {
                MessageBox.Show(this, "请选择用户信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("是否确认删除？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    new DataAccess.tb_User().DeleteForUserID(_IndexList);

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "删除用户信息失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }

        private void btnFingerPrint_Click(object sender, EventArgs e)
        {
            groupBoxX2.Visible = !groupBoxX2.Visible;
            try
            {
                if (fingerPrintMng == null)
                {
                    fingerPrintMng = new Relations.Control.UC.fingerPrintMng();
                    fingerPrintMng.Dock = DockStyle.Fill;
                    groupBoxX2.Controls.Add(fingerPrintMng);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Dictionary<string, string> GetLogisticList()
        {
            Dictionary<string, string> _Result = new Dictionary<string, string>();

            _Result.Add("userName", checkName.Checked ? txtName.Text : string.Empty);

            if (checkRole.Checked)
                _Result.Add("roleId", cmbRole.SelectedValue.ToString() != "-1" ? cmbRole.SelectedValue.ToString() : string.Empty);
            else
                _Result.Add("roleId", string.Empty);

            return _Result;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserInfoForm _UserInfoFrom = new UserInfoForm();
            if (_UserInfoFrom.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    new DataAccess.tb_User().SaveUserInfo(_UserInfoFrom.UserInfo);

                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "保存用户信息失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (dgvUserInfo.SelectedRows.Count <= 0)
            {
                MessageBox.Show(this, "请选择用户信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Model.DB.tb_User _Temp = new DataAccess.tb_User().GetForUserId(Convert.ToInt32(dgvUserInfo.SelectedRows[0].Cells["userId"].Value.ToString()));

            if (_Temp == null)
            {
                MessageBox.Show(this, "用户下无相关信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Forms.UserInfoForm _UserInfoForm = new Forms.UserInfoForm();
            _UserInfoForm.ModifyInfo(_Temp);

            if (_UserInfoForm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    _UserInfoForm.UserInfo.userId = _Temp.userId;
                    new DataAccess.tb_User().UpdateUserInfo(_UserInfoForm.UserInfo);
                    
                    btnSearch_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改送检单位失败，错误信息：" + ex.ToString());
                }
            }
        }
    }
}
